﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.UserAd
{
    public partial class AdAdd : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindAdPosition();
            }
        }

        #region 加载我的广告位
        private void BindAdPosition()
        {
            Users uinfo = UserBase.GetCurrentUser();
            UserAdPositionHelper uaphelper = new UserAdPositionHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            c.Add(CriteriaType.MoreThanEquals, "EndDate", System.DateTime.Now);
            List<UserAdPosition> list = uaphelper.QueryUserAdPositionBySearch(c, new Order[] { new Order("Orders", OrderMode.Asc) });

            ddlAdPosition.DataSource = list;
            ddlAdPosition.DataTextField = "AdPositionName";
            ddlAdPosition.DataValueField = "AdPositionID";
            ddlAdPosition.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlAdPosition.Items.Insert(0, li);

            if (list.Count == 0)
            {
                lblRemark.Text = "<a href=\"MyAdPosition.aspx\" style=\"color:Red\">请先购买广告位</a>";
            }
        }
        #endregion

        #region 提交广告
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Users uinfo = UserBase.GetCurrentUser();
            Ad ainfo = new Ad();
            int adtype = 1;
            if (rbImage.Checked)
            {
                adtype = 1;//图片
            }
            else if (rbScript.Checked)
            {
                adtype = 2;//文字
            }

            string adpositionid = ddlAdPosition.SelectedValue;
            string adpositionname = ddlAdPosition.SelectedItem.Text;
            string adname = txtAdName.Text.Trim();
            string linkurl = txtLinkURL.Text.Trim();
            string adscript = txtScript.Value.Trim();//文字内容
            string newFileName = "";
            //圈子图片
            if (fuImage.HasFile)
            {
                string FileName = fuImage.FileName.ToString();
                string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
                string filename = FileName.Replace(ExtenName, "");
                newFileName = filename + System.Guid.NewGuid().ToString() + ExtenName;
                if (ExtenName == ".jpg" || ExtenName == ".gif" || ExtenName == ".png" || ExtenName == ".jpeg")
                {
                    string filePath = Server.MapPath("..\\AdImages\\") + newFileName;//合并两个路径为上传到服务器上的全路径,其中System.Web.HttpContext.Current.Request.MapPath("~/upload/")将指定的虚拟路径映射到物理路径
                    fuImage.SaveAs(filePath);
                }
                else
                {
                    Tools.Alert("图片格式不支持！");
                    return;
                }
            }
            if (adtype == 1)
            {
                if (newFileName == "")
                {
                    Tools.Alert("请上传广告图片!");
                    return;
                }
            }
            else if (adtype == 2)
            {
                if (string.IsNullOrEmpty(adscript))
                {
                    Tools.Alert("请填写广告文字!");
                    return;
                }
            }

            ainfo.AdPositionID = adpositionid;
            ainfo.AdPositionName = adpositionname;
            ainfo.AdType = adtype;
            if (adtype == 1)//图片
            {
                ainfo.ImageURL = newFileName;
            }
            else if (adtype == 2)//文字
            {
                ainfo.AdScript = adscript;
            }
            
            ainfo.AdName = adname;
            ainfo.LinkURL = linkurl;
            
            ainfo.UsersID = uinfo.ID;
            ainfo.NickName = uinfo.NickName;
            //系统账号添加不需要审批
            string systemaduserid = System.Configuration.ConfigurationSettings.AppSettings["systemaduserid"] + "";
            if (uinfo.ID == systemaduserid)
            {
                ainfo.Status = 1;
            }
            else
            {
                ainfo.Status = 0;
            }
            ainfo.ShowCount = 0;
            ainfo.ClickCount = 0;
            AdHelper ahelper = new AdHelper();
            ahelper.AddAd(ainfo);
            Tools.AlertAndRedirect("广告添加成功", "MyAd.aspx");
            return;
        }
        #endregion

        #region 广告位改变
        protected void ddlAdPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            string positionid = ddlAdPosition.SelectedValue;
            if (positionid == "-1")
            {
                lblRemark.Text = "请选择广告位";
            }
            else
            {
                //加载该广告位描述
                AdPosition apinfo = new AdPosition();
                AdPositionHelper aphelper = new AdPositionHelper();
                apinfo = aphelper.GetAdPosition(positionid);
                string remark = apinfo.Remark;
                lblRemark.Text = remark;
            }
        }
        #endregion

        #region 图片按钮改变
        protected void rbImage_CheckedChanged(object sender, EventArgs e)
        {
            if (rbImage.Checked)
            {
                liimage.Visible = true;
                liscript.Visible = false;
            }
            else
            {
                liimage.Visible = false;
                liscript.Visible = true;
            }
        }
        #endregion

        #region 文字按钮改变
        protected void rbScript_CheckedChanged(object sender, EventArgs e)
        {
            if (rbScript.Checked)
            {
                liimage.Visible = false;
                liscript.Visible = true;
            }
            else
            {
                liimage.Visible = true;
                liscript.Visible = false;
            }
        }
        #endregion
    }
}